/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jeff.springWebTestNG.aspect;

import com.jeff.springWebTestNG.annotation.TellHimGoToHell;
import com.jeff.springWebTestNG.enums.AnnotationDefinedAction;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * A basic example already coded as BasicServiceAsepct.doServiceBeforeCheck(), 
 * here is to explore deeper usage of annotation.
 * @author zhou.zhefu
 */
@Component
@Aspect
public class AnnotatedCutAspect {
    @Pointcut("execution(* com.jeff.springWebTestNG.service.*.*(..))")
    public void anyAnnotatedOperation() {}
    
    @Around("anyAnnotatedOperation() && @annotation(tellHimGoToHell)")
    public void tellHimGoToHell(ProceedingJoinPoint pjp, TellHimGoToHell tellHimGoToHell) {
        try {
            long startTime = System.currentTimeMillis();
            boolean noProfiling = false;
            AnnotationDefinedAction action = tellHimGoToHell.action();
            switch (action) {
                case None:
                    pjp.proceed();
                    break;
                case Do:
                    System.out.println("Please goto Hell...");
                    pjp.proceed();
                    break;
                case DontDo:
                    //do nothing but print screen
                    noProfiling = true;
                    System.out.println("Not allowed to proceed a goto-hell operation");
                    break;
                default:
                    pjp.proceed();
            }
            long endTime = System.currentTimeMillis();
            
            if (!noProfiling)
                System.out.println("This Operation takes " + (endTime - startTime) + " milliseconds to execute.");
        } catch (Throwable ex) {
            Logger.getLogger(AnnotatedCutAspect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
